import os

import mmcv
# using a pre-trained detector. 预训练配置
from mmcv import Config


# root_path=r"/project/train/src_repo/mmdetection/" # 要改的

root_path=r"/home/deepin/Documents/ji_pingtai/mmdetection/" # 要改的

cfg_path=root_path+'work_dirs/yolov3_d53_fp16_mstrain-608_273e_coco/yolov3_d53_fp16_mstrain-608_273e_coco.py'

cfg = Config.fromfile(cfg_path)

from mmdet.apis import set_random_seed, inference_detector, show_result_pyplot

cfg.load_from = root_path+'checkpoints/yolov3_d53_fp16_mstrain-608_273e_coco_20210517_213542-4bc34944.pth'



cfg.device='cuda' # 'ConfigDict' object has no attribute 'device'


cfg.work_dir = 'work_dir'
# Set seed thus the results are more reproducible #播种，结果更具可重复性
cfg.seed = 0
set_random_seed(0, deterministic=False)
cfg.gpu_ids = range(1)

cfg.evaluation.interval = 10 # 评估间隔，以减少评估时间
cfg.evaluation.save_best='auto' # 保存最佳best的pth

cfg.checkpoint_config.interval = 51  # 保存间隔:  设置检查点：，以降低存储成本
cfg.runner.max_epochs = 50  # 保存间隔:  设置检查点：，以降低存储成本


#训练----------------------------------------------------
from mmdet.datasets import build_dataset
from mmdet.models import build_detector
from mmdet.apis import train_detector


# Build dataset 构建 数据集
print(cfg.data.train)
datasets = [build_dataset(cfg.data.train)]

# Build the detector 构建 识别
model = build_detector(cfg.model)

# Add an attribute for visualization convenience 添加属性以方便可视化，模型的类别
model.CLASSES = datasets[0].CLASSES

# Create work_dir 文件夹
mmcv.mkdir_or_exist(os.path.abspath(cfg.work_dir))

train_detector(model, datasets, cfg, distributed=False, validate=True) # -------进行----训练-----


# # # 查看 评估结果 load tensorboard in colab
# # %load_ext tensorboard
# #
# # # see curves in tensorboard
# # tensorboard --logdir ./tutorial_exps  #--------运行这个看看 数的 曲线



